package com.ledim.fragment.base;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ledim.adapter.base.BaseRvDataPresenter;
import com.ledim.widget.recyclerview.BaseQuickAdapter;
import com.ledim.widget.recyclerview.loadmore.LedimLoadMoreView;
import com.ledim.widget.recyclerview.loadmore.LoadMoreView;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/12/6.
 * Rv ListFragment 基类(包含下拉刷新以及上拉加载)
 */

public abstract class BaseRvListFragment extends LeDimBaseFragment {

    protected BaseRvDataPresenter mRvPresenter;
    protected RecyclerView mRecyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        initContentView(view, savedInstanceState);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRvPresenter = new BaseRvDataPresenter(view, getAdapter(), mRecyclerView) {
            @Override
            public void getData() {
                BaseRvListFragment.this.getData();
            }
        };
        if (enableLoadMore()) {
            mRvPresenter.initLoadMoreView(getLoadMoreView());
        }
        if (enablePullRefresh()) {
            mRvPresenter.initRefreshView();
        }
        // 第一次进来时自动刷新 默认true
        if (enterAutoLoadData()) {
            mRvPresenter.enterRefresh();
        }
        return view;
    }

    /**
     * @return 是否首次进入直接加载数据
     */
    protected boolean enterAutoLoadData() {
        return true;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getActivity());
    }

    protected LoadMoreView getLoadMoreView() {
        return new LedimLoadMoreView();
    }

    /**
     * @return 允许上拉加载
     */
    protected boolean enableLoadMore() {
        return true;
    }

    /**
     * @return 允许下拉刷新
     */
    protected boolean enablePullRefresh() {
        return true;
    }

    public abstract BaseQuickAdapter getAdapter();

    public abstract void getData();

}
